"""
认证模块路由
"""
from fastapi import APIRouter, HTTPException, Depends
from pydantic import BaseModel

from ..dependencies import get_current_user, create_access_token
from ..config import AUTH_PASSWORD

router = APIRouter(prefix="/api/auth", tags=["认证"])


class LoginRequest(BaseModel):
    password: str


class LoginResponse(BaseModel):
    token: str
    message: str


@router.post("/login", response_model=LoginResponse)
async def login(request: LoginRequest):
    """用户登录"""
    if request.password != AUTH_PASSWORD:
        raise HTTPException(
            status_code=401,
            detail="密码错误"
        )
    
    # 创建JWT token
    access_token = create_access_token(data={"sub": "admin"})
    
    return LoginResponse(
        token=access_token,
        message="登录成功"
    )


@router.get("/verify")
async def verify_auth(current_user: dict = Depends(get_current_user)):
    """验证token是否有效"""
    return {"valid": True, "user": current_user}

